package com.mxx.qh.utils;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
 * 作者：刘江飞
 * 时间：2020/10/16  16:10
 * 邮箱：642371742Z@qq.com
 * 描述：权限申请工具类
 **/
public class PermissionUtil {
    private static final int REQUEST_CODE_PERMISSION = 1000;
    private IPermissionListener requestPermissionResultListener;
    private static volatile PermissionUtil instance;

    public static PermissionUtil getInstance() {
        if (instance == null) {
            synchronized (PermissionUtil.class) {
                if (instance == null) {
                    instance = new PermissionUtil();
                }
            }
        }
        return instance;
    }

    /**
     * @param context            上下文对象
     * @param permissionListener 权限申请的结果回调
     * @param permissions        所需权限的集合
     */
    public void requestPermission(Activity context, IPermissionListener permissionListener
            , String... permissions) {
        try {
            requestPermissionResultListener = permissionListener;
            int targetSdkVersion = 0;
            final PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            targetSdkVersion = info.applicationInfo.targetSdkVersion;//获取应用的Target版本
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //Build.VERSION.SDK_INT是获取当前手机版本 Build.VERSION_CODES.M为6.0系统
                //如果系统>=6.0
                if (targetSdkVersion >= Build.VERSION_CODES.M) {
                    //第 1 步: 检查是否有相应的权限
                    boolean isAllGranted = checkPermissionAllGranted(context, permissions);
                    if (isAllGranted) {
                        permissionListener.allPermissionGranted();
                        return;
                    }
                    // 一次请求多个权限, 如果其他有权限是已经授予的将会自动忽略掉
                    ActivityCompat.requestPermissions(context,
                            permissions, REQUEST_CODE_PERMISSION);
                }
            } else {
                permissionListener.allPermissionGranted();
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(context, "检测该设备版本失败", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void handlerActivityResult(int requestCode, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            boolean isAllGranted = true;
            // 判断是否所有的权限都已经授予了
            for (int grant : grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    isAllGranted = false;
                    break;
                }
            }
            if (isAllGranted) {
                // 所有的权限都授予了
                requestPermissionResultListener.allPermissionGranted();
            } else {
                // 弹出对话框告诉用户需要权限的原因, 并引导用户去应用权限管理中手动打开权限按钮
                //容易判断错
                //MyDialog("提示", "某些权限未开启,请手动开启", 1) ;
                requestPermissionResultListener.partPermissionRefuse();
            }
        }
    }

    /**
     * 检查是否拥有指定的所有权限
     */
    private static boolean checkPermissionAllGranted(Activity context, String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                // 只要有一个权限没有被授予, 则直接返回 false
                //Log.e("err","权限"+permission+"没有授权");
                return false;
            }
        }
        return true;
    }


    public interface IPermissionListener {
        /**
         * 所有权限被允许
         */
        void allPermissionGranted();

        /**
         * 部分权限被拒绝
         */
        void partPermissionRefuse();
    }
}
